package com.meitianhui.convenitenbuy.module.wallet.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.main.model.bean.EventBean;
import com.meitianhui.convenitenbuy.module.wallet.adapter.WalletPacketsAdapter;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletPacketsContract;
import com.meitianhui.convenitenbuy.module.wallet.di.DaggerWalletComponent;
import com.meitianhui.convenitenbuy.module.wallet.di.WalletModule;
import com.meitianhui.convenitenbuy.module.wallet.model.RedPacketBean;
import com.meitianhui.convenitenbuy.module.wallet.presenter.WalletPacketsPresenter;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.RxUtils;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class WalletRedPacketsActivity extends BaseToolbarActivity implements WalletPacketsContract.View, BaseQuickAdapter.RequestLoadMoreListener {

    @Inject
    WalletPacketsPresenter presenter;
    private RelativeLayout mViewOpen;
    private RecyclerView mRv;
    private LinearLayout mLlNone;
    private int pageNum = 1;
    private WalletPacketsAdapter mAdapter;
    private RedPacketBean currentClickItem;

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTbTitle(getString(R.string.wallet_wallet_red_packet));
        DaggerWalletComponent.builder().appComponent(App.getInstance().getAppComponent()).walletModule(new WalletModule(this)).build().inject(this);
        initId();
        initInfo();
        resisterPush();
    }

    private void resisterPush() {
        Disposable disposable = RxBus.get().toFlowable(EventBean.class).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<EventBean>() {
            @Override
            public void accept(EventBean bean) throws Exception {
                if (bean != null) {
                    if (bean.type == Constants.EVENT_TYPE_REFRESH) {
                        pageNum = 1;
                        initInfo();
                    }
                }
            }
        });
        mCompositeDisposable.add(disposable);
    }

    private void initInfo() {
        presenter.getRedPackets(pageNum);
    }

    private void initId() {
        mViewOpen = getViewById(R.id.rl_wallet_packet_bg);
        mLlNone = getViewById(R.id.ll_wallet_packets_none);
        mRv = getViewById(R.id.rv_wallet_packets);
        LinearLayoutManager mLinearManager = new LinearLayoutManager(this);
        mLinearManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRv.setLayoutManager(mLinearManager);
        RxUtils.bindEvents(getViewById(R.id.rl_wallet_packet_open), this);
        RxUtils.bindEvents(getViewById(R.id.iv_wallet_packet_close), this);
        RxUtils.bindEvents(mViewOpen, this);
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
            case R.id.rl_wallet_packet_open:
                clickRedPacket();
                break;
            case R.id.iv_wallet_packet_close:
                mViewOpen.setVisibility(View.GONE);
                break;
        }
    }

    private void clickRedPacket() {
        presenter.openRedPacket(currentClickItem);
    }

    @Override
    public void showError(String msg) {

    }


    @Override
    protected void onRefreshData() {
        pageNum = 1;
        initInfo();
    }

    @Override
    protected void clickMenu() {
    }

    @Override
    protected int setView() {
        return R.layout.activity_wallet_red_packets;
    }

    @Override
    public void openRedPacketResult(RedPacketBean packetBean) {
        jumpToPacketDetail(packetBean);
        mViewOpen.setVisibility(View.GONE);
        pageNum = 1;
        initInfo();
        RxBus.get().post(new EventBean(Constants.EVENT_TYPE_REFRESH));
    }

    @Override
    public void getRedPacketsBack(List<RedPacketBean> list) {
        refreshFinish();
        if (list == null || list.isEmpty())
            mLlNone.setVisibility(View.VISIBLE);
        if (mAdapter == null) {
            mAdapter = new WalletPacketsAdapter(WalletRedPacketsActivity.this, list);
            mRv.setAdapter(mAdapter);
            mAdapter.setOnPacketsItemClickListener(new WalletPacketsAdapter.OnPacketItemClickListener() {
                @Override
                public void onEnablePacketItemClick(int position, RedPacketBean bean) {
                    jumpToPacketDetail(bean);
                }

                @Override
                public void onDisablePacketItemClick(int position, RedPacketBean bean) {
                    mViewOpen.setVisibility(View.VISIBLE);
                    currentClickItem = bean;
                }
            });
            mAdapter.setOnLoadMoreListener(this, mRv);
        } else {
            if (mAdapter.isLoading())
                mAdapter.loadMoreComplete();
            if (pageNum == 1) {
                if (list != null) {
                    mAdapter.replaceData(list);
                }
            } else {
                mAdapter.addData(list);
            }
        }

        if (list != null) {
            mAdapter.setEnableLoadMore(list.size() < 20 ? false : true);
            mLlNone.setVisibility(list.isEmpty() && pageNum == 1 ? View.VISIBLE : View.GONE);
            refreshFinish();
        }
    }

    @Override
    public void onLoadMoreRequested() {
        pageNum++;
        initInfo();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mAdapter != null && mAdapter.isLoading())
            mAdapter.loadMoreEnd();
        refreshFinish();
    }

    private void jumpToPacketDetail(RedPacketBean bean) {
        Intent intent = new Intent(WalletRedPacketsActivity.this, WalletRedPacketActivity.class);
        intent.putExtra(Constants.ARGUMENT, bean);
        startActivity(intent);
    }


}
